/**
 * Generali Solutions d'assurances - Tous droits réservés &copy; 2007 - 2010
 */
package fr.generali.accueilclient.client.assembler.output;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import fr.generali.accueilclient.client.om.IPieceIdentite;
import fr.generali.accueilclient.client.om.impl.PieceIdentite;
import fr.generali.refservcommuns.rce.demat.servperscom.dct.gestiondocuments.DtoPieceIdentite;

/**
 * Transforme un {@link DtoPieceIdentite} en {@link IPieceIdentite}
 * 
 * @author selrhazala
 */
@Component("dtoPieceIdentite2PieceIdentiteConverter")
public class DtoPieceIdentite2PieceIdentiteConverter implements Converter<DtoPieceIdentite, IPieceIdentite> {

    /**
     * LOGGER
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(DtoPieceIdentite2PieceIdentiteConverter.class);

    /**
     * {@inheritDoc}
     */
    public IPieceIdentite convert(DtoPieceIdentite input) {
        LOGGER.info("assemble");
        IPieceIdentite pieceIdentite = null;
        if (input != null) {

            pieceIdentite = new PieceIdentite();

            if (input.getDateDelivrance() != null) {
                pieceIdentite.setDateDelivrance(input.getDateDelivrance().toGregorianCalendar().getTime());
            }

            pieceIdentite.setDateFinRetrait(null);

            if (input.getPeriodeValidite() != null && input.getPeriodeValidite().getEndDate() != null) {
                pieceIdentite.setDateFinValidite(input.getPeriodeValidite().getEndDate().toGregorianCalendar()
                                .getTime());
            }

            pieceIdentite.setLieuDelivrance(input.getLieuDelivrance());
            pieceIdentite.setNumero(input.getNumeroOfficiel());
            pieceIdentite.setPaysDelivrance(input.getPaysAutoriteDelivrante());

            if (input.getPermisConduire() != null) {
                pieceIdentite.setStatut(input.getPermisConduire().getStatut());
                pieceIdentite.setCategorie(input.getPermisConduire().getCategorie());
            }

            if (input.getCarteIdentite() != null) {
                pieceIdentite.setStatut(input.getCarteIdentite().getStatut());
            }

            pieceIdentite.setType(input.getTypePiece());
        }

        return pieceIdentite;
    }
}
